package com.young.consumer.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.young.common.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.Charset;

/**
 * @author ：young
 * @date ：Created in 2019/7/9
 * @description：HttpClient
 */
@RestController
@RequestMapping(value = "consumer")
public class HttpClientController {
    @RequestMapping(value = "httpClientFile", method = RequestMethod.POST)
    public void uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse httpResponse = null;
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000000).build();
        /**
         * 此处需要加负载均衡
         */
        HttpPost httpPost = new HttpPost("http://127.0.0.1:9528/app/uploadattach");
        httpPost.setConfig(requestConfig);
        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
        multipartEntityBuilder.setCharset(Charset.forName("UTF-8"));
        byte[] cache = file.getBytes();
        File tmpFile = new File(file.getOriginalFilename());
        OutputStream out = new FileOutputStream(tmpFile);
        out.write(cache);
        multipartEntityBuilder.setContentType(ContentType.MULTIPART_FORM_DATA);
        multipartEntityBuilder.addBinaryBody("file", tmpFile);
        ObjectMapper mapper = new ObjectMapper();
        multipartEntityBuilder.addTextBody("user", mapper.writeValueAsString(new User()));
        HttpEntity httpEntity = multipartEntityBuilder.build();
        httpPost.setEntity(httpEntity);
        httpResponse = httpClient.execute(httpPost);
        HttpEntity responseEntity = httpResponse.getEntity();
        out.close();
        tmpFile.delete();
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode == 200) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(responseEntity.getContent()));
            StringBuffer buffer = new StringBuffer();
            String str = "";
            while (!StringUtils.isEmpty(str = reader.readLine())) {
                buffer.append(str);
            }
            System.out.println(buffer.toString());
        }
        httpClient.close();
        if (httpResponse != null) {
            httpResponse.close();
        }
    }
}
